﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class Solution16
    {
        public IList<IList<int>> CombinationSum2(int[] candidates, int target)
        {
            List<IList<int>> Ans = new List<IList<int>>();
            List<int> temperList = new List<int>();
            Array.Sort(candidates);
            getAnswer(Ans,candidates,target,temperList, 0);
            return Ans;
        }
        public void getAnswer(List<IList<int>> Ans, int[] candidates, int target, List<int> temperList, int index)
        {
            if (target==0)
            {             
                if (!Ans.Contains(temperList))
                {
                    Ans.Add(temperList);
                    return;
                }               
            }
            for (int i = index; i < candidates.Length; i++)
            {
                if (candidates[index] <= target)
                {
                    List<int> list = new List<int>(temperList);                 
                    list.Add(candidates[i]);
                    getAnswer(Ans, candidates, target - candidates[i], list, i);
                }
                else
                {
                    break;
                }
            }
        }
    }
}
